package com.good.child.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.good.child.R;
import com.good.child.SplashActivity;
import com.good.child.iview.LoginIView;
import com.good.child.pressent.LoginPresenter;
import com.good.child.ui.adapter.GuideAdapter;
import com.good.child.ui.utils.DimensionUtil;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.tool.TActivityUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create on: 2024/7/21
 * by Mr.Zhu
 * Description: TODO
 */
public class WelcomeActivity extends BaseActivity<LoginIView, LoginPresenter> implements LoginIView {

    @BindView(R.id.view_page)
    ViewPager viewpage;
    @BindView(R.id.guideActivity_ll_points)
    LinearLayout llPoints;
    @BindView(R.id.guideActivity_bt_experience_now)
    Button btnExperienceNow;

    private int[] imgs = {R.mipmap.splash_1,R.mipmap.splash_2,R.mipmap.splash_3};
    private int lastPointIndex = 0;
    @Override
    public void onUserLoginSuccess() {

    }

    @Override
    protected LoginPresenter initPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        initData();
        initEvent();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcom);
        setStatusBar(true, false);
        setAndroidNativeLightStatusBar(this, true);
        ButterKnife.bind(this);
        initView();
    }

    private void initData() {
        ArrayList<ImageView> imageViews = new ArrayList<>();
        ImageView imageView = null;
        View pointView = null;
        LinearLayout.LayoutParams layoutParams = null;
        for (int i = 0; i < imgs.length; i++) {
            //添加图片
            imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(imgs[i]);
            imageViews.add(imageView);

            //添加指针
            pointView = new View(this);
            pointView.setBackgroundResource(R.drawable.point_seleoter);
            //指针的宽度和高度
            layoutParams = new LinearLayout.LayoutParams(DimensionUtil.dpToPx(5), DimensionUtil.dpToPx(5));

            if (i==0){
                pointView.setEnabled(true);
            } else {
                //指针距离左边的间距
                layoutParams.leftMargin = DimensionUtil.dpToPx(5);
                pointView.setEnabled(false);
            }

            llPoints.addView(pointView,layoutParams);

        }
        GuideAdapter guideAdapter = new GuideAdapter(imageViews);
        viewpage.setAdapter(guideAdapter);
    }


    private void initEvent() {
        viewpage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }
            @Override
            public void onPageSelected(int i) {
                llPoints.getChildAt(lastPointIndex).setEnabled(false);
                //将当前的点选中
                llPoints.getChildAt(i).setEnabled(true);

                if (i==2){
                    btnExperienceNow.setVisibility(View.VISIBLE);
                }else {
                    btnExperienceNow.setVisibility(View.GONE);
                }
                lastPointIndex = i;
            }
            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        btnExperienceNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TActivityUtils.jumpToActivity(WelcomeActivity.this, LoginActivity.class);
                finish();
            }
        });


    }
}
